package com.decent.ejfadmin.order.service.impl;

import com.decent.ejfadmin.common.bean.TrySellRedisBean;
import com.decent.ejfadmin.common.enums.SupRedisKey;
import com.decent.ejfadmin.common.enums.SupplyCardError;
import com.decent.ejfadmin.common.enums.SupplyCardStatus;
import com.decent.ejfadmin.order.dao.CardSupplyOrderDao;
import com.decent.ejfadmin.order.dao.OrderDao;
import com.decent.ejfadmin.order.dao.TrySellBatchDao;
import com.decent.ejfadmin.order.service.SupplyCardOrderService;
import com.decent.ejfadmin.utils.SimpleCacheUtil;
import com.decent.ejfadmin.common.bean.SupplyCard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * 销卡订单管理
 *
 * @author 王元鑫
 * @date 2016/12/8
 */
@Service("SupplyCardOrderService")
public class SupplyCardOrderServiceImpl implements SupplyCardOrderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplyCardOrderServiceImpl.class);
    @Autowired
    private CardSupplyOrderDao cardSupplyOrderDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private TrySellBatchDao trySellBatchDao;

    /**
     * 面值预判不符慢速试充卡密结算处理
     *
     * @param orderId     订单号
     * @param settlePrice 结算面值
     */
    @Override
    public void specialDealJudgeWrongFaceCard(String orderId, BigDecimal settlePrice) {
        SupplyCard card = orderDao.querySupplyOrderByOrderId(orderId);
        TrySellRedisBean bean =
                (TrySellRedisBean) SimpleCacheUtil.getObjectJson(SupRedisKey.SUP_TRY_SELL_BEAN + card.getBatchId() + card.getProductType(), TrySellRedisBean.class);

        LOGGER.info("[{}]提交redis[{}]", orderId, bean);
        if (bean == null) {
            LOGGER.info("线上提交redis[{}]回调[{}],reidskey全为空直接返回", orderId);
            return;
        }

        //结算面值无误||销卡失败,不做处理,直接返回
        if (card.getFace().compareTo(settlePrice) == 0
                || !card.getStatus().equals(SupplyCardStatus.success.getStatus())) {
            //批次内没有慢速试充待返回订单,该批次允许下一次试充
            if (cardSupplyOrderDao.queryBatchStatusOrders(card.getBatchId(), card.getProductType()) == 0) {
                LOGGER.info("允许下一次试充[{}]", card);
                bean.setCommitted(false);
                SimpleCacheUtil.setObjectJson(SupRedisKey.SUP_TRY_SELL_BEAN + card.getBatchId() + card.getProductType(), bean,
                        (int) TimeUnit.DAYS.toSeconds(15L));
            }
            return;
        }
        //慢速试充批次内不包含,直接返回
        if (trySellBatchDao.queryBatch(card.getBatchId(), card.getProductType()) == 0) {
            return;
        }
        HashMap<String, Object> resultMap = orderDao.queryWrongCardRate(card, SupplyCardStatus.waitSlowTrySell.getStatus());
        //没有待慢速试充的卡
        if (resultMap.get("preJudge") == null || resultMap.get("preJudge").equals(BigDecimal.ZERO)) {
            return;
        }
        LOGGER.info("[{}]慢速试充批次出现问题卡[{}],上传面值[{}],预判面值[{}],结算面值[{}]",
                card.getBatchId(), card.getCardNumber(), card.getFace(), card.getJudgeFace(), card.getSettlePrice());
        orderDao.batchWrongFaceSupplyCard(card, SupplyCardStatus.waitSlowTrySell.getStatus(),
                SupplyCardError.BATCH_WRONG_FACE);
        bean.setCommitted(false);
        SimpleCacheUtil.setObjectJson(SupRedisKey.SUP_TRY_SELL_BEAN + card.getBatchId() + card.getProductType(), bean,
                (int) TimeUnit.DAYS.toSeconds(15L));
    }
}
